#include "YGFileManager.h"

#if (YG_OS_TYPE == YG_OS_TYPE_WINDOWSMOBILE)

	#include "YGFileManagerWM6.h"
	#include "YGFileStreamWM6.h"

#elif (YG_OS_TYPE == YG_OS_TYPE_ANDROID)

	#include "YGFileManagerAndroid.h"
	#include "YGFileStreamAndroid.h"

#endif

namespace YoghurtGum
{

	FileManager* FileManager::s_Instance = NULL;

	// =========================================
	// FileManager
	// =========================================

	void FileManager::SetWorkingDirectory(const char* a_Directory, uint32 a_Length)
	{
		strcpy(m_WorkingDirectory, a_Directory);
		m_WorkingDirectoryLength = strlen(m_WorkingDirectory);
	}

	char* FileManager::GetWorkingDirectory() 
	{ 
		if (m_WorkingDirectoryLength == 0) { return NULL; }

		return m_WorkingDirectory; 
	}

	char* FileManager::AddWorkingDirectory(char* a_Path)
	{
		if (m_WorkingDirectoryLength == 0) { return NULL; }

		unsigned int length = m_WorkingDirectoryLength + strlen(a_Path);
		char* result = new char[length];

		strcpy(result, m_WorkingDirectory);
		strcat(result, a_Path);

		return result;
	}

	bool FileManager::GetFileExists(const char* a_FileName)
	{
		FILE* result = fopen(AddWorkingDirectory((char*)a_FileName), "r");
		bool exists = (result != NULL);
		if (exists) { fclose(result); }
		return (exists);
	}

	uint32 FileManager::GetWorkingDirectoryLength()
	{
		return m_WorkingDirectoryLength;
	}

#if (YG_OS_TYPE == YG_OS_TYPE_ANDROID)

	/*FileStream* FileManager::Open(const char* a_File, uint32 a_Options)
	{

	}*/

#elif (YG_OS_TYPE == YG_OS_TYPE_WINDOWSMOBILE)

	/*FileStream* FileManager::Open(const char* a_File, uint32 a_Options)
	{
		FileStream* result = new FileStreamWM6();
		result->Open(a_File, a_Options);
		return result;
	}*/

#endif

	FileManager& FileManager::Get()
	{
		if (!s_Instance) { s_Instance = new FileManager(); }
		return *s_Instance;
	}

	void FileManager::SetInstance(FileManager* a_Instance)
	{
		s_Instance = a_Instance;
	}

	FileManager::FileManager()
	{
		m_WorkingDirectoryLength = 0;
	}

	FileManager::~FileManager()
	{

	}

}; // namespace YoghurtGum